package com.cn.tous.security.dtos;

import com.alibaba.nacos.api.model.v2.ErrorCode;
import lombok.Getter;
import lombok.ToString;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author mengwei
 * @description Result
 * @createDate 2025/7/26 14:18
 */
@ToString
public class Result <T> implements Serializable {

    @Serial
    private static final long serialVersionUID = -8400397321618442101L;

    private int code;
    private String message;
    @Getter
    private T data;

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Result() {

    }

    public Result(T data) {
        this(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), data);
    }

    public Result(Integer code, String message) {
        this(code, message, null);
    }

    public Result(ErrorCode errorCode) {
        this(errorCode.getCode(), errorCode.getMsg(), null);
    }

    public Result(ErrorCode errorCode, T data) {
        this(errorCode.getCode(), errorCode.getMsg(), data);
    }

    public static <T> Result<T> success() {
        return new Result<>();
    }
    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }
    public static <T> Result<T> success(T data, String message) {
        return new Result<>(ErrorCode.SUCCESS.getCode(), message, data);
    }

    public static <T> Result<T> failure( String message) {
        return new Result<>(ErrorCode.SERVER_ERROR.getCode(), message, null);
    }


    public Integer getCode() {
        return this.code;
    }

}
